import { FSMState } from './State';
import IFSMEvent from './IFSMEvent'
export class FSMSystem {
    private _state: string = '';
    private _target: IFSMEvent = null;

    public constructor(target?: IFSMEvent) {
        this._target = target;
    }

    public get state() {
        return this._state;
    }

    public set state(value: string) {
        if (this._state !== value) {
            if (this._target)
                this._target.onStateExit(this._state);

            this._state = value;
            if (this._target && value !== '')
                this._target.onStateEnter(this._state);
        }
    }


    /**
     * 开始状态机
     * @param initState 初始状态
     */
    public start(initState: string) {
        this.state = initState;
    }

    /**
     * 更新状态机
     */
    public update() {
        if (this._target && this._state !== '') {
            this._target.onStateStay(this._state);
        }
    }
}
